package cn.poka.device;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android_serialport_api.SerialPort;
import cn.poka.util.DeviceInfo;
import poka_global_constant.GlobalConstant;

/**
 * 无线串口设备
 * @author digl
 */
public class WirelessDevice {

	/** 无线串口实例 */
	private static WirelessDevice wirelessDevice;

	/** 无线串口序列化接口 */
	private static SerialPort wirelessSerialPort;

	public static SerialPort getWirelessSerialPort() {
		return wirelessSerialPort;
	}

	public static void setWirelessSerialPort(SerialPort wirelessSerialPort) {
		WirelessDevice.wirelessSerialPort = wirelessSerialPort;
	}

	/** 通过无线串口序列化接口，得到的命令输入流 */
	private static InputStream mInput;

	/** 通过无线串口序列化接口，得到的命令输出流 */
	private static OutputStream mOutput;

	/** 无线设备IO口,通过设备IO口可以进行上电、断电操作 */
	private static GPIO wirelessPowerIO;

	private final byte WIRELESS_HEAD = (byte)0x41;

	private WirelessDevice(){

	}



	/**
	 * 获取无线串口单例实例，如果无线串口的单例实例为空，则进行初始化
	 * @return
	 */
	public static WirelessDevice getInstance(){
		if(wirelessDevice == null){
			if(wirelessSerialPort == null){
				File wirelessFile = new File(DeviceInfo.WIRELESS_FILE);
				try {
					wirelessSerialPort = new SerialPort(wirelessFile, DeviceInfo.WIRELESS_DEVICE_KEY, 0);

					wirelessPowerIO = new GPIO(GlobalConstant.IO_WIRELESS_POWER);

					mInput = wirelessSerialPort.getInputStream();
					mOutput = wirelessSerialPort.getOutputStream();

					wirelessDevice = new WirelessDevice();
				} catch (SecurityException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

		return wirelessDevice;
	}

	/**
	 * 给无线串口设备上电
	 */
	public void enable(){
		if(wirelessPowerIO != null){
			wirelessPowerIO.setGPIO(GlobalConstant.ENABLE_IO);
		}
	}

	/**
	 * 给无线串口设备断电
	 */
	public void disenable(){
		if(wirelessPowerIO != null){
			wirelessPowerIO.setGPIO(GlobalConstant.DISABLE_IO);
		}
	}

	/**
	 * 关闭无线串口设备
	 */
	public void close(){
		if(wirelessSerialPort != null){
			//关闭无线串口序列化接口
			wirelessSerialPort.close();
			wirelessSerialPort = null;
			try {
				//关闭无线串口输入、输出流
				mInput.close();
				mOutput.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		if(wirelessDevice != null){
			wirelessDevice = null;
		}
	}

	/**
	 * 发送指令
	 * @param cmd
	 */
	public void sendCMD(byte[] cmd){
		try {
			//通过无线串口输出流发送命令
			mOutput.write(cmd);
			mOutput.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 读取响应帧
	 * @return
	 */
	public byte[] read(){
		byte[] buf = new byte[50];
		if(wirelessSerialPort != null){
			wirelessSerialPort.serialRead(buf, buf.length);
		}
		return buf;
//		Log.d("tag__", "来这里了aaa");
//		byte[] responseData = null;
//		byte[] response = null;
//		int available = 0 ;
//		int index = 0;
//		int headIndex = 0;
//		//500ms内轮询无数据则，无数据返回
//		try {
//			while(index < 10){
//				Thread.sleep(20);
//				available = mInput.available();
//				Log.d("tag__", "来这里了bbb:" + available);
//				//缓冲区有数据
//				if(available > 7) break;
//				index++;
//			}
//			if(available > 0){
//				Log.d("tag__", "来这里了1");
//				responseData = new byte[available];
//				mInput.read(responseData);
//				for(int i = 0; i < available; i++){
//					Log.d("tag__", "来这里了2:" + Integer.toHexString(0xFF & responseData[i]));
//	//				if(responseData[i] == WIRELESS_HEAD){
//	//					headIndex = i;
//	//					break;
//	//				}
//				}
//				response = new byte[available - headIndex];
//				System.arraycopy(responseData, headIndex, response, 0, response.length);
//				
//			}
//		} catch (Exception e1) {
//			e1.printStackTrace();
//		}
//		
//		return response;
	}


}













































































































